const app = getApp()
const api = require("../../api/address.js")
const order = require('../../api/orders.js')
var pass = ""
var payObjs = []
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbarData: {
      showCapsule: true, //是否显示左上角图标
      title: '下单', //导航栏 中间的标题
      isHome: false
    },
    height: app.globalData.height * 2 + 20 ,  
    ids: [],
    addressId: 0,
    priceTotal: 0,
    selectTotal: 0,
    start: '',
    deliveryTime: '',
    end: '',
    address: [],
    orders: {},
    showModal: false,
    ad:'',
    res_address:[{id:0, name:"还没有地址"}],
    total: 0,
    remark: '没有备注',
    remarkSize: 4,
    getPass: function(e){
      pass = e
      return e;
    },
    onConfirm: function(e){
      var i = payObjs.map(item => {
        item.payPassword = pass
        return item
      })
      order.pay(i).then(res=>{
        wx.navigateBack({
          delta: 0,
        })
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 2000
        })
      })
    },
    rules: [{
          name: 'deliveryTime',
          rules: [{required: true, message: '配送时间必填'}],
      }, {
          name: 'addressId',
          rules: [{required: true, message: '配送地址必填'}],
      }, {
        name: 'remark',
        rules: [{required: true, message: '备注必填'}],
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var day1 = new Date();
    var s1 = day1.format("yyyy-MM-dd");
    day1.setDate(day1.getDate() + 7);
    var s2 = day1.format("yyyy-MM-dd");
    this.setData({
      deliveryTime: s1,
      start: s1,
      end: s2
    })
    const that = this
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('payIds', function(data) {
      that.setData({...data})
    })
    api.list(1).then(res=>{
      this.setData({
        address: res.data.data,
        total: res.data.total,
      })
      this.setData({
        res_address: this.data.address.map(item=>{
          return {
            id: item.addressId,
            name: item.province+item.city+item.area+item.street
          }
        })
      })
      this.setData({
        addressId: this.data.res_address[0].id,
        ad: this.data.res_address[0].name
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  inputChange: function(e){
    const {field} = e.currentTarget.dataset
    var value = e.detail.value
    if (field == 'remark'){
      var i = value.length
      if (i >= 100){
        wx.showToast({
          title: '输入超出限制',
          icon: 'none'
        })
        this.setData(
          {remark: this.data.remark}
        )
        return
      }
      this.setData({
        remark: value,
        remarkSize: value.length
      })
    }
  },
  bindDateChange: function(e){
    this.setData({
      deliveryTime: e.detail.value,
    })
  },
  bindAddressChange:function(e){
    const address = this.data.address[e.detail.value]
    const {addressId} = address
    this.setData({
      addressId: addressId,
      ad: this.data.res_address[e.detail.value].name
    })
  },
  pay: function(){
    const {remark, ids,addressId,deliveryTime } = this.data
    this.setData({
      orders: {remark,deliveryTime,remark, addressId}
    })
    if(!this.data.addressId){
      wx.showToast({
        title: "地址必选",
        icon: 'none'
      })
      return
    }
    if(!this.data.deliveryTime){
      wx.showToast({
        title: "配送时间必须",
        icon: 'none'
      })
      return
    }
    if(!this.data.remark){
      wx.showToast({
        title: "备注必填",
        icon: 'none'
      })
      return
    }
    this.selectComponent('#form').validate((valid, errors) => {
      if (!valid) {
        var err = errors[0].message
          wx.showToast({
            title: err,
            icon: 'none'
          })
      }else{
        payObjs = ids.map(item =>{
          return {
            "addressId": addressId,
            "cakeId": item,
            "deliveryTime": deliveryTime,
            "remark": remark
          }
        })
        this.setData({
          showModal:true
        })
      }
    })
  }
})